<?php
namespace app\admin\model;

use think\Model;

class OrderProfit extends Model
{

        public function selectInfo($where,$field = '*') {
            return $this->where($where)->field($field)->select()->toArray();
        }


    /**
     * 获取佣金总列表
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @author hey_heyang
     * @return  array()
     */
    public function getTgTotalList($start_time,$end_time,$page_size=10,$sort='desc',$type=false){
        $another_model = new AddAnotherMoneyInfo;
        if($start_time && $end_time){
            $time1 = strtotime($start_time);
            $time2 = strtotime($end_time.'+ 1 day');
            $condition1['or.create_time'] = array('between',$time1.','.$time2);
            $condition2['payment_time'] = array('between',$time1.','.$time2);
            $condition2['payment_time'] = array('between',$time1.','.$time2);
        }elseif($start_time){
            $condition1['or.create_time'] = array('eqt',strtotime($start_time));
            $condition2['payment_time'] = array('eqt',strtotime($start_time));
        }elseif($end_time){
            $condition1['or.create_time'] = array('elt',strtotime($end_time.'+ 1 day')-1);
            $condition2['payment_time'] = array('elt',strtotime($end_time.'+ 1 day')-1);
            $condition3['payment_time'] = array('elt',strtotime($end_time.'+ 1 day')-1);
        }
        $condition1['status']=1;  //佣金到账成功
        $field1 = 'FROM_UNIXTIME(or.create_time) as time,m.username,m.mobile,or.money,"发放" as "type"';
        $condition2['type']=3;  //平台充值的佣金
        $field2 = 'FROM_UNIXTIME(payment_time) as time,"公司" as username," "as mobile,money,"充值" as "type"';
        $condition3['type'] =4; //推广结算金额
        $field3 = 'FROM_UNIXTIME(payment_time) as time,"公司" as username," "as mobile,money,"结算" as "type"';
        $data1=$this->alias('or')->join('member m','m.id=or.member_id')->where($condition1)->field($field1)->buildSql();
        $data2=$another_model->where($condition2)->field($field2)->buildSql();
        $data3=$another_model->where($condition3)->field($field3)->union([$data1])->union([$data2])->buildSql();
        if($type){
            $data = $this->table($data3 . ' a')->order('time '.$sort)->select()->toArray();
        }else{
            $data = $this->table($data3 . ' a')->order('time '.$sort)->paginate($page_size,false,page_param());
        }

        return $data;
    }



    /**
     * 获取直推佣金列表
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @author hey_heyang
     * @return  array()
     */
    public function getMemberYjList($start_time,$end_time,$page_size=10,$sort='desc',$type=false){
        if($start_time && $end_time){
            $time1 = strtotime($start_time);
            $time2 = strtotime($end_time.'+ 1 day');
            $condition['or.create_time'] = array('between',$time1.','.$time2);
        }elseif($start_time){
            $condition['or.create_time'] = array('eqt',strtotime($start_time));
        }elseif($end_time){
            $condition['or.create_time'] = array('elt',strtotime($end_time.'+ 1 day')-1);
        }
        $condition['status']=1;  //佣金到账成功
        $field = 'FROM_UNIXTIME(or.create_time) as time,m.username,m.mobile,or.money,"发放" as "type"';
        if($type){
            $data=$this->alias('or')->join('member m','m.id=or.member_id')->where($condition)->field($field)->order('time '.$sort)->select()->toArray();
        }else{
            $data=$this->alias('or')->join('member m','m.id=or.member_id')->where($condition)->field($field)->order('time '.$sort)->paginate($page_size,false,page_param());
        }
        return $data;
    }

        /*
         * 会员推广明细列表
         * @param mixed $where 查询条件
         * @param mixed $startTime 开始时间
         * @param mixed endTime 结束时间
         * @param mixed $page 分页
         * @author cpy(1442435897@qq.com)
         * @return array
         */
        public function memberPromotionDetails($where,$startTime,$endTime,$sort = 'create_time desc',$field = '*') {
            return $this->where($where)->field($field)->whereTime('create_time','between',[$startTime,$endTime])->order($sort)->paginate('', false, page_param());
        }

}